import Functions = require('core/Functions.module');

export = WidgetUse;

class WidgetUse{
    WidgetConfig;
    panels = new Array();
    halfpanels = new Array();

    useWidget(widgetId){
        $.each(this.WidgetConfig, function (index, item) {
            $.each(item.data,function(index,item){
            if (item.id == widgetId) {
                var target = {data:null};
                target.data = {};
                target.data["id"] = item["id"];
                target.data["label"] = item["label"];
                target.data["widget"] = item["widget"];
                target.data["main"] = item["main"];
                target.data["in-panel"] = item["inPanel"];
                target.data["panel"] = item["panel"];
                var paneltype = item["panel"];
                var panel = null;
                if(paneltype=="HalfPanel")
                panel = this.halfpanels[target.data["id"]];
                else
                  panel = this.panels[target.data["id"]];
                if (panel) {
                   // this.bug('模块[' + target.data["label"] + ']正在使用当中!');
                    if (panel.focus) {
                        panel.focus();
                    }
                    return;
                }
                this.loadWidget(target);
            }
           }.bind(this));
        }.bind(this));
    }

   loadWidget(target) {
        var _this = this;
        if (target.data["in-panel"] === true) {
          if (target.data['panel'] === "HalfPanel") {
                require({}, ["widgets/HalfPanel/widget"], function (HalfPanel) {
                    //_this.log('加载面板模块:' + target.data('widget'));
                    _this.halfpanels[target.data['id']] = new HalfPanel({
                         widgetPath: "widgets/HalfPanel",
                        afterDestroyCallback: _this.destroyWidget.bind(_this, target.data['id'])
                    }, {
                        panelTitle: target.data['label'],
                        panelUniqClassName: target.data['id'],
                        innerWidgetPath: target.data['widget'],
                        innerWidgetMain: target.data['main']
                    });
                });
            }
            else {
                require({}, ["widgets/Panel/widget"], function (Panel) {
                    //_this.log('加载面板模块:' + target.data['widget']);
                    _this.panels[target.data['id']] = new Panel({
                         widgetPath: "widgets/Panel",
                        afterDestroyCallback: _this.destroyWidget.bind(_this, target.data['id'])
                    }, {
                        panelTitle: target.data['label'],
                        panelUniqClassName: target.data['id'],
                        innerWidgetPath: target.data['widget'],
                        innerWidgetMain: target.data['main']
                    });
                });
            }
        }
        else {
            require({}, [Functions.concatUrl(target.data["widget"], target.data["main"])], function (Widget) {
                //_this.log('加载模块:' + target.data["widget"]);
                _this.panels[target.data["id"]] = new Widget({
                     widgetPath: target.data["widget"],
                    afterDestroyCallback: _this.destroyWidget.bind(_this, target.data["id"])
                });
            });
        }
    };
    
    closeWidget (widgetID){
         var panels = this.panels.concat(this.halfpanels);
         if (panels[widgetID] !== null && panels[widgetID] !== undefined)
         {
            panels[widgetID].close();
            this.destroyWidget(widgetID);
         }
    };
    
    getWidget(widgetID)
    {
            var panels = this.panels.concat(this.halfpanels);
         var panel = panels[widgetID];
          return panel;
    };
   
    destroyWidget(widgetID) {
       
        this.panels[widgetID] = null;
        delete this.panels[widgetID];
         this.halfpanels[widgetID] = null;
        delete this.halfpanels[widgetID];
       // this.log('已销毁对象:ID = ' + widgetID);
    };
    
    /*addToDom(data) {
        var htmlString = _.template(this.template)(data);
        this.setHtml(htmlString);
    }*/
}